<!-- 洗衣车 -->
<template>
	<view class="page_box" style="background: rgb(246, 246, 246);">
		<!-- <shopro-navbar backIconName="" backText="" :backTextStyle="{
					color: '#121516',
					fontSize: '36rpx',
					fontWeight: '500'
				}" :background="{
					background: BASE_URL+'/uploads/20231014/4a1f50fe125e3681d81c6f939631dfe2.png'
				}">
		</shopro-navbar> -->
		<view
			style="margin: 0;padding: 25rpx;position: absolute;top: 0;height: 437rpx;width: 100vw;background-size: 100% 100%;"
			:style="'background-image: url('+BASE_URL+'/uploads/20231014/4a1f50fe125e3681d81c6f939631dfe2.png);'">
		</view>
		<view class="" :style="[navbarInnerStyles]">

		</view>
		<!-- 总数 -->
		<view class="head_box" v-show="!isEmpty" style="position: relative;z-index: 1;">
			<view class="tool-box u-flex u-row-between">
				<view class="count-box">
					共
					<text class="all-num">{{ cartList.length }}</text>
					件衣物
				</view>
				<button class="u-reset-button set-btn" @tap="isTool = !isTool">{{ isTool ? '完成' : '管理' }}</button>
			</view>
		</view>

		<view class="content_box" style="position: relative;z-index: 1;">
			<!-- 列表 -->
			<u-checkbox-group @change="checkboxGroupChange" activeColor="#0099E5" v-if="!isEmpty">
				<view class="collect-list u-flex u-row-left u-col-center" v-for="(g, index) in cartList" :key="index">
					<u-checkbox class="u-p-l-10" :name="g.goods_id" shape="circle" v-model="g.checked" v-if="isTool">
						<view style="height: 160rpx"></view>
					</u-checkbox>
					<view class="goods-wrap">
						<view class="lose-box"
							v-if="g.cart_type === 'invalid' || (g.cart_type === 'activity' && !isActivityPay)">
							<text v-if="g.cart_type === 'invalid'" class="iconfont icon-yishixiao"></text>
							<view v-if="g.cart_type === 'activity' && !isActivityPay"
								class="invalid-tips u-flex u-row-center u-col-center">活动商品,仅支持单独购买</view>
						</view>
						<shopro-mini-card :end="true" :detail="g.goods" :image="g.goods.image" :title="g.goods.title"
							@click="$Router.push({ path: '/pages/goods/detail', query: { id: g.goods.id } })">
							<template #describe>
								<view class="order-sku u-ellipsis-1" v-if="g.sku_price && g.sku_price.goods_sku_text">
									<text
										class="order-num">{{ g.sku_price && g.sku_price.goods_sku_text ? g.sku_price.goods_sku_text : '' }}</text>
								</view>
							</template>
							<template #cardBottom>
								<view class="order-price-box u-flex u-row-between" style="padding-right: 16rpx;"
									@tap.stop>
									<view class="x-f">
										<text
											class="order-price font-OPPOSANS">￥{{ g.goods.discounts ? (g.sku_price ? g.sku_price.price : 0) / g.goods.discounts * 10 : (g.sku_price ? g.sku_price.price : 0) }}</text>

										<view class="x-f" style="margin-left: 14rpx;
									height: 36rpx;line-height: 36rpx;padding: 0 0 0 12rpx;
									background: #F95641;
									border-radius: 8rpx;" v-if="g.goods.discounts">
											<view class="origin-price" style="
									font-size: 22rpx;
									font-family: PingFang SC;
									font-weight: 400;
									color: #FFFFFF;">{{ g.goods.discounts }}折后</view>
											<view class="price" style="min-width: 62rpx;
									height: 32rpx;
									line-height: 28rpx;text-align: center;
									background: #FFFFFF;
									border: 2px solid #F95641;
									border-radius: 8rpx;
									font-size: 18rpx;
									font-family: Source Han Sans CN;
									font-weight: 400;
									color: #F95641;">{{ (g.sku_price ? g.sku_price.price : 0) }}</view>
										</view>
									</view>

									<u-number-box :value="g.goods_num" :long-press="false" :size="32" :min="0" :step="1"
										:index="index"
										:max="!g.sku_price ? 0: (g.sku_price.stock > 999 ? 999 : g.sku_price.stock)"
										@min="onMin(g)" @minus="changeNum($event, g)" @plus="changeNum($event, g)"
										@change="changeNum($event, g)">
									</u-number-box>
								</view>

								<view class="tags x-f" style="flex-wrap: wrap;margin-top: 8rpx;"
									v-if="parseFloat(g.goods.discounts) == 10" @tap.stop="$Router.push({ path: '/pages/user/card' })">
									<view class="tag">
										前往购买打折卡
									</view>
								</view>
							</template>
						</shopro-mini-card>
					</view>
				</view>
			</u-checkbox-group>

			<!-- 数据为空 -->
			<shopro-empty v-if="isEmpty" :image="$IMG_URL + '/imgs/empty/empty_cart.png'" tipText="洗衣车空空如也,快去逛逛吧~">
			</shopro-empty>
		</view>

		<!-- 底部按钮 -->
		<view class="foot_box " v-show="!isEmpty">
			<view class="tools-box u-flex u-row-between">
				<u-checkbox @change="onAllSel" activeColor="#0099E5" shape="circle" :value="allSelected">
					全选（{{ totalCount.totalNum }}）</u-checkbox>
				<view class="u-flex">
					<view class="price font-OPPOSANS" v-show="!isTool">￥{{ totalCount.totalPrice.toFixed(2) }}
					</view>
					<button class="u-reset-button pay-btn" :disabled="!isSel" v-show="!isTool" @tap="onPay">结算</button>
					<button class="u-reset-button del-btn" v-show="isTool" @tap="goodsDelete">删除</button>
				</view>
			</view>
		</view>
		<shopro-tabbar></shopro-tabbar>
	</view>
</template>

<script>
	import {
		HAS_LIVE,
		BASE_URL
	} from '@/env';
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	let timer = null;
	let systemInfo = uni.getSystemInfoSync();
	let menuButtonInfo = {};
	// #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
	menuButtonInfo = uni.getMenuButtonBoundingClientRect();
	// #endif
	export default {
		components: {},
		data() {
			return {
				statusBarHeight: systemInfo.statusBarHeight,
				BASE_URL: BASE_URL,
				maxStep: 999,
				isTool: false
			};
		},
		computed: {
			...mapGetters(['totalCount', 'isSel', 'isActivityPay', 'cartList', 'allSelected', 'authType', 'isLogin']),
			isEmpty() {
				return !this.cartList.length;
			},
			// 导航栏内部盒子的样式
			navbarInnerStyles() {
				let style = {};
				style.height = this.statusBarHeight + this.navbarHeight + 'px';
				// #ifdef MP
				let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
				style.marginRight = rightButtonWidth + 'px';
				// #endif
				return style;
			},
			navbarHeight() {
				// #ifdef APP-PLUS || H5
				return 44;
				// #endif
				// #ifdef MP
				return systemInfo.platform == 'ios' ? 44 : 48;
				// #endif
			}
		},
		onShow() {
			this.isLogin && this.getCartList();
		},
		onHide() {
			this.isTool = false;
		},
		methods: {
			...mapActions(['getCartList', 'changeCartList']),

			// 到达最小值
			onMin(g) {
				uni.showModal({
					title: '删除提示',
					confirmColor: '#0099E5',
					content: `是否确认从洗衣车中删除此商品?`,
					success: res => {
						res.confirm && this.changeCartList({
							ids: [g.id],
							art: 'delete'
						});
					}
				});
			},
			// 更改商品数
			async changeNum(e, g) {
				uni.showLoading({
					mask: true
				});
				e.value > 0 && this.changeCartList({
					ids: [g.id],
					goodsNum: e.value,
					art: 'change'
				});
				uni.hideLoading();
				await this.getCartList();
			},

			// 单选
			checkboxGroupChange(e) {
				this.$store.commit('checkCartList');
			},

			// 路由跳转
			jump(path, parmas) {
				this.$Router.push({
					path: path,
					query: parmas
				});
			},

			// 全选
			onAllSel() {
				let that = this;
				that.$store.commit('changeAllSellect'); //按钮切换全选。
				that.$store.commit('getAllSellectCartList', that.allSelected); //列表全选
			},

			// 结算
			onPay() {
				let that = this;
				let {
					cartList
				} = this;
				if (this.isSel) {
					let confirmcartList = [];
					let isActivity = false;
					for (let item of this.cartList) {
						if (item.checked) {
							if (item.cart_type === 'invalid') {
								this.$u.toast('商品已失效');
								return false;
							}
							if (item.cart_type === 'activity') {
								isActivity = true;
							}
							confirmcartList.push({
								goods_id: item.goods_id,
								sku_price_id: item.sku_price_id,
								goods_price: item.sku_price ? item.sku_price.price : 0,
								goods_num: item.goods_num
							});
						}
					}
					if (confirmcartList.length > 1 && isActivity) {
						this.$u.toast('活动商品只能单独购买');
						return false;
					}
					that.jump('/pages/order/confirm', {
						goodsList: confirmcartList,
						from: 'cart'
					});
				}
			},
			// 删除
			goodsDelete() {
				let that = this;
				let {
					cartList
				} = this;
				let selectedIdsArray = [];
				cartList.map(item => {
					if (item.checked) {
						selectedIdsArray.push(item.id);
					}
				});
				this.changeCartList({
					ids: selectedIdsArray,
					art: 'delete'
				});
			}
		}
	};
</script>

<style lang="scss">
	// 总计商品
	.head_box {
		.tool-box {
			height: 90rpx;
			padding: 0 30rpx;
			// background: #f7f5f6;

			.count-box {
				font-size: 26rpx;
				color: #999;
				font-size: 26rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #666666;

				.all-num {
					color: #0099E5;
				}
			}

			.set-btn {
				background: none;
				font-size: 26rpx;
				color: #0099E5;
				font-size: 28rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #333333;
			}
		}
	}

	// 空白页
	.empty-box {
		flex: 1;
		width: 100%;
		height: 100%;
	}

	// 洗衣车项
	.collect-list {
		background: #fff;
		width: 750rpx;
		margin-bottom: 20rpx;
		padding: 20rpx 10rpx;
		margin: 25rpx;
		width: 700rpx;
		height: 230rpx;
		background: #FFFFFF;
		border-radius: 10rpx;

		// 商品卡片包裹
		.goods-wrap {
			position: relative;

			.order-sku {
				font-size: 24rpx;
				font-weight: 400;
				color: rgba(153, 153, 153, 1);
				width: 440rpx;
				margin-bottom: 20rpx;

				.order-num {
					margin-right: 10rpx;
				}
			}

			.order-price-box {
				.status-btn {
					height: 32rpx;
					border: 1rpx solid #0099E5;
					border-radius: 15rpx;
					font-size: 20rpx;
					font-weight: 400;
					color: #0099E5;
					padding: 0 10rpx;
					margin-left: 20rpx;
					background: rgba(0, 153, 229, 0.16);
				}

				.order-price {
					font-size: 26rpx;
					font-weight: 600;
					color: #ff0000;
				}
			}

			.lose-box {
				position: absolute;
				z-index: 10;
				width: 100%;
				height: 100%;
				background-color: rgba(#fff, 0.8);

				.icon-yishixiao {
					position: absolute;
					bottom: 0rpx;
					right: 80rpx;
					font-size: 140rpx;
					line-height: 140rpx;
					color: #dbdbdb;
					transform: rotate(-30deg);
				}

				.invalid-tips {
					position: absolute;
					top: 0;
					right: 0;
					left: 0;
					bottom: 0;
					margin: auto;
					width: 400rpx;
					height: 60rpx;
					border-radius: 30rpx;
					color: #fff;
					background-color: rgba(#000, 0.35);
				}
			}
		}

		.tag-box {
			.tag1 {
				line-height: 36rpx;
				padding: 0 8rpx;
				font-size: 18rpx;
				color: rgba(#fff, 0.9);
				background: #0099E5;
				display: inline-block;
				box-sizing: border-box;
			}

			.tag2 {
				line-height: 34rpx;
				padding: 0 8rpx;
				font-size: 18rpx;
				color: rgba(#0099E5, 0.9);
				background: #fff;
				border-top: 1rpx solid #0099E5;
				border-right: 1rpx solid #0099E5;
				border-bottom: 1rpx solid #0099E5;
				display: inline-block;
				box-sizing: border-box;
			}
		}

		.price-box {
			width: 420rpx;

			.price {
				color: #e1212b;
			}
		}
	}

	.tools-box {
		height: 100rpx;
		width: 750rpx;
		padding: 0 20rpx;
		background: #fff;

		.check-all {
			font-size: 26rpx;

			.check-all-radio {
				transform: scale(0.7);
				color: #0099E5;
			}
		}

		.price {
			color: #ff0000;
			font-size: 500;
			margin-right: 30rpx;
		}

		.pay-btn {
			width: 200rpx;
			line-height: 70rpx;
			background: linear-gradient(90deg, #F95641, #F95641);
			// box-shadow: 0px 7rpx 6rpx 0px rgba(0, 153, 229, 0.22);
			border-radius: 35rpx;
			padding: 0;
			color: rgba(#fff, 0.9);
		}

		.del-btn {
			width: 200rpx;
			line-height: 70rpx;
			background: linear-gradient(90deg, rgba(244, 71, 57, 1) 0%, rgba(255, 102, 0, 1) 100%);
			border-radius: 35rpx;
			padding: 0;
			color: rgba(#fff, 0.9);
		}
	}

	/deep/ .goods-title {
		font-size: 28rpx;
		font-weight: 500;
		color: rgba(51, 51, 51, 1);
		width: 400rpx !important;
		line-height: 40rpx;
	}

	/deep/ .goods_ri {
		width: 488rpx !important;
	}

	.tag {
		padding: 0 12rpx;
		height: 32rpx;
		background: #EDF9FF;
		border-radius: 5rpx;
		font-size: 20rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #51A6DC;
		margin-right: 14rpx;
		margin-bottom: 10rpx;
	}

	.tag {
		background: #FDEAE6;
		color: #F95641;
	}
</style>